#include "config.h"
#include <iostream>
/*!
 * \brief loadConfig
 * \param confFileName
 * \param conf
 * \return -1 : error
 *         0 : ok
 */
int loadConfig(QString confFileName, Config &conf) {
    QFile file(confFileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return -1;

    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
//        std::cout << line.toStdString() << std::endl;
        if (line.size() == 0 || line.at(0) == '#') {
            continue;
        }
        QStringList list = line.split(QLatin1Char(' '));
        if (list.size() < 2) {
            std::cout << "Wrong conf {" << line.toStdString() << "}\n";
            return -1;
        }
        if (list.at(0) == "dirs") {
            for (int i = 1; i < list.size(); i++) {
                conf.dirs.append(list.at(i));
            }
        } else if (list.at(0) == "tagsFile") {
            conf.tagsFile = list.at(1);
        } else if (list.at(0) == "powerBoot") {
            if (list.at(1) == "true") {
                conf.powerBoot = true;
            } else {
                conf.powerBoot = false;
            }
        } else if (list.at(0) == "tagBtnTime") {
            conf.tagBtnTime = list.at(1).toInt();
        } else if (list.at(0) == "unTagBtnTime") {
            conf.unTagBtnTime = list.at(1).toInt();
        } else if (list.at(0) == "tagBtnName") {
            for (int i = 1; i < list.size(); i++) {
                conf.tagBtnName.append(list.at(i));
            }
        } else if (list.at(0) == "unTagBtnName") {
            for (int i = 1; i < list.size(); i++) {
                conf.unTagBtnName.append(list.at(i));
            }
        } else {
            std::cout << "Unknown conf {" << line.toStdString() << "}\n";
            return -1;
        }
    }
    return 0;
}
